
game.state.addState("main")
game.state.registerLoopFunc("main", "main_loop")
game.state.registerKeyboardKeyDownFunc("main", "main_keyDown")
game.state.registerKeyboardKeyReleasedFunc("main", "main_keyReleased")

text = ""

local camera = {x = 20, y = 20}
local move = {x = 0, y = 0}
local speed = 20

layers = createLayers(4, 200, 200)
setTile(layers[1], 10,10,4,29)
setTile(layers[1], 11,10,5,29)
setTile(layers[1], 10,9,4,28)
setTile(layers[1], 11,9,5,28)
layers[2] = newForest(10,10)
setCastle(layers[3], 10, 10)

function main_loop()
	main_update()
	
	drawMap(camera.x,camera.y, layers[0], tileset)
	drawMap(camera.x,camera.y, layers[1], tileset)
	drawMap(camera.x,camera.y, layers[2], trees)
	drawMap(camera.x,camera.y, layers[3], castles)
	
	gui.draw()
	font:draw(50,200, text)
	
	--show mouse position
	mouseX, mouseY = game.mouse.getPosition()
	s = "X:"..mouseX
	font:draw(20, 560, s)
	s = "Y:"..mouseY
	font:draw(20, 535, s)
	
	main_drawLabels()
end

function main_drawLabels()
	font:draw(50, 100, "SetForest")
	font:draw(200, 100, "disabled")
	font:draw(50, 50, "Choose Tile")
end


function main_update()
	camera.x = camera.x+move.x*speed
	camera.y = camera.y+move.y*speed
	
	--updateForest(forest)
end


function main_keyDown(key)
	if(key == "space") then
		text = text.." "
	elseif(key == "enter") then
		text = text..""
	elseif(key == "left shift") then
		text = text..""
	elseif(key == "backspace") then
		text = string.sub(text, 0, string.len(text)-1)
	elseif(key == "escape") then
		game.quit()
	elseif(isTextKey(key)==true) then 
		text = text..key
	elseif(key == "up") then
		move.y = -1
	elseif(key == "down") then
		move.y = 1
	elseif(key == "left") then
		move.x = 1
	elseif(key == "right") then
		move.x = -1 
	end
end


function main_keyReleased(key)
	if(key == "up") then
		move.y = 0
	elseif(key == "down") then
		move.y = 0
	elseif(key == "left") then
		move.x = 0
	elseif(key == "right") then
		move.x = 0
	end
end
